/*
 * Created on Mar 22, 2010
 */
package ro.letsdoitromania.forms;

import java.util.Vector;
import javax.microedition.lcdui.*;
import com.mgmaps.app.APPCommands;
import com.mgmaps.tools.UITools;
import com.mgmaps.utils.LS;
import ro.letsdoitromania.Commands;
import ro.letsdoitromania.LDIRCanvas;
import ro.letsdoitromania.LDIRMidlet;
import ro.letsdoitromania.Settings;

public class LoginForm implements CommandListener {
  private Form form;
  
  private static LoginForm instance;
  private String language;
  
  private StringItem siLogin;
  private StringItem siError;
  private TextField tfUser;
  private TextField tfPass;
  
  public static LoginForm getInstance() {
    if (instance == null)
      instance = new LoginForm();
    return instance;
  }
  
  private LoginForm() {
    language = Settings.language;
  }
  
  private void resetForm() {
    siError = null;
    siLogin = null;
    tfUser = null;
    tfPass = null;
  }
  
  public void show() {
    show(null);
  }
  
  public void show(String errmsg) {
    if (language != Settings.language)
      resetForm();
    
    if (errmsg != null) {
      if (siError == null)
        siError = new StringItem(LS.get("app.form.error"), errmsg);
      else
        siError.setText(errmsg);
    }
    
    if (siLogin == null)
      siLogin = new StringItem("", LS.get("ldir.login.inform"));
    
    if (tfUser == null)
      tfUser = new TextField(LS.get("app.settings.username"), Settings.username,
          30, TextField.ANY);
    else
      tfUser.setString(Settings.username);
    tfUser.setInitialInputMode("MIDP_LOWERCASE_LATIN");
    
    if (tfPass == null)
      tfPass = new TextField(LS.get("app.settings.password"), Settings.password,
          30, TextField.ANY | TextField.PASSWORD);
    else
      tfPass.setString(Settings.password);
    tfPass.setInitialInputMode("MIDP_LOWERCASE_LATIN");

    Vector itemVector = new Vector();
    if (errmsg != null)
      itemVector.addElement(siError);
    itemVector.addElement(siLogin);
    itemVector.addElement(tfUser);
    itemVector.addElement(tfPass);
    
    final Item[] items = new Item[itemVector.size()];
    itemVector.copyInto(items);
    itemVector.removeAllElements();

    form = UITools.buildForm(form, LS.get("ldir.login.title"), items,
        new Command[] { Commands.cinstance.continueCommand, APPCommands.instance.backCommand }, this);
    
    Display.getDisplay(LDIRMidlet.instance).setCurrent(form);    
  }

  public void commandAction(Command cmd, Displayable disp) {
    if (cmd == APPCommands.instance.backCommand)
      Display.getDisplay(LDIRMidlet.instance).setCurrent(LDIRCanvas.instance);
    else if (cmd == Commands.cinstance.continueCommand) {
      // save username and password
      Settings.username = tfUser.getString().trim();
      Settings.password = tfPass.getString().trim();
      Settings.usersalt = "";
      Settings.save();
      
      LDIRCanvas.cinstance.startUpload();
    }
  }
}
